#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>

/** 
 * Client sends only one message to server,
 * gets an answer and closes itself.
 */

int main() {
  int sock;
  struct sockaddr_in addr;
  char buf[1024];
  char message[1024];
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    return errno;
  }
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(32000);
  #define SERVER_ADDR "127.0.0.1"
  inet_aton(SERVER_ADDR, &addr.sin_addr.s_addr);
  sprintf(message, "Client with id: %d", getpid());
  sendto(sock, message, sizeof(message), 0, 
         (struct sockaddr *)&addr, sizeof(addr));
  int n = recvfrom(sock, buf, sizeof(message), 0, 0, 0);
  buf[n] = 0;
  printf("%s\n", buf);
  close(sock);
  return 0;
}
